﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace HEXXTOOLS
{
    public partial class Form1 : Form
    {
        private HEXDocument mHexDoc;
        byte[] BufferBinary;
        char[] BufferText;
        Stream SourceFile;
        int TextSelStart;
        int TextSelLength;
        int BinarySelStart;
        int BinarySelLength;

        public Form1()
        {
            InitializeComponent();
            mHexDoc = new HEXDocument();
        }       

        private void WriteTextData()
        {
            this.richTextBox2.Clear();
            this.richTextBox1.Clear();
            StringBuilder text = new StringBuilder();
            StringBuilder binary = new StringBuilder();
            int rowcount = BufferBinary.Length / 16;
            if (BufferBinary.Length % 16 > 0)
            {
                rowcount++;
            }            
            for (int i = 0; i < rowcount; i++)
            {
                StringBuilder linetext = new StringBuilder(16);
                StringBuilder linebinary = new StringBuilder(16);
                for (int j = 0; j < 16 && BufferBinary.Length > (16 * i) + j; j++)
                {
                    linetext.Append(BufferText[(16 * i) + j]);
                    linebinary.AppendFormat("{0:X2}", BufferBinary[(16 * i) + j]);
                    linebinary.Append("\t");
                    linetext.Replace('\0', '.');
                    linetext.Replace('\r', '.');
                    linetext.Replace('\n', '.');
                }
                text.AppendLine(linetext.ToString());
                binary.AppendLine(linebinary.ToString());
            }
            
            this.richTextBox2.AppendText(text.ToString());
            this.richTextBox1.AppendText(binary.ToString());
            
        }

        private void CalculateSelectionBinaryData()
        {
            int LineStart = TextSelStart / 16;
            int PositionStart = TextSelStart - (16 * LineStart);
            BinarySelStart = (LineStart * 16 * 3 - 2) + (PositionStart * 3);
            BinarySelLength = TextSelLength * 3;
            this.richTextBox1.Select(BinarySelStart, BinarySelLength);
            this.richTextBox1.Focus();
        }
        

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                SourceFile = openFileDialog1.OpenFile();
                BufferBinary = new byte[SourceFile.Length];
                SourceFile.Read(BufferBinary, 0, BufferBinary.Length);                
                BufferText = Encoding.ASCII.GetChars(BufferBinary, 0, BufferBinary.Length);               
                this.richTextBox2.Clear();
                this.richTextBox1.Clear();
                WriteTextData();               
            }
            
        }

       

        private void cutToolStripButton_Click(object sender, EventArgs e)
        {
            TextSelStart = this.richTextBox2.SelectionStart;
            TextSelLength = this.richTextBox2.SelectionLength;
            //BinarySelStart = this.richTextBox1.SelectionStart;
            //BinarySelLength = this.richTextBox1.SelectionLength;
            CalculateSelectionBinaryData();
        }
    }
}
